package com.woniuxy.mini;

import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversation;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationOutput;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationParam;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationResult;
import com.alibaba.dashscope.common.MultiModalMessage;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.exception.UploadFileException;
import com.alibaba.dashscope.utils.JsonUtils;
import com.woniuxy.util.ResponseData;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@RestController
public class ImgChatController {
//    @Value("${dashscope.api-key}")
//    private String dashScopeApiKey;
    @GetMapping("/getImgChat")
    public ResponseData getImgChat(
            @RequestParam(value = "image") String image,
            @RequestParam(value = "text") String text
    ) throws NoApiKeyException, UploadFileException {
        MultiModalConversation conv = new MultiModalConversation();
        MultiModalMessage userMessage = MultiModalMessage.builder().role(Role.USER.getValue())
                .content(Arrays.asList(
                        Collections.singletonMap("image", image),
//                        Collections.singletonMap("image", "https://dashscope.oss-cn-beijing.aliyuncs.com/images/tiger.png"),
//                        Collections.singletonMap("image", "https://dashscope.oss-cn-beijing.aliyuncs.com/images/rabbit.png"),
                        Collections.singletonMap("text", text))).build();
        MultiModalConversationParam param = MultiModalConversationParam.builder()
                // 若没有配置环境变量，请用百炼API Key将下行替换为：.apiKey("sk-xxx")
//                .apiKey(System.getenv("sk-6c59f6a99b5943adba495771bb40e5b9"))
                .apiKey("sk-6c59f6a99b5943adba495771bb40e5b9")
                // 此处以qwen-vl-plus为例，可按需更换模型名称。模型列表：https://help.aliyun.com/zh/model-studio/getting-started/models
                .model("qwen-vl-plus")
                .message(userMessage)
                .build();
        MultiModalConversationResult result = conv.call(param);
        String content = extractTextFromResult(result);
        return ResponseData.ok(content);
    }
    private static String extractTextFromResult(MultiModalConversationResult result) {
        if (result != null && result.getOutput() != null && !result.getOutput().getChoices().isEmpty()) {
            MultiModalConversationOutput.Choice choice = result.getOutput().getChoices().get(0);
            if (choice.getMessage() != null && !choice.getMessage().getContent().isEmpty()) {
                Object content = choice.getMessage().getContent().get(0);
                if (content instanceof Map) {
                    Map<String, String> contentMap = (Map<String, String>) content;
                    return contentMap.get("text");  // ✅ 只返回 text 的值
                }
            }
        }
        return null; // 如果没有找到 text 内容则返回 null 或者抛出异常
    }
}
